package com.yanqu.road.server.manger.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.EscortDartTypeInfo;
import com.yanqu.road.entity.beautyescort.EscortServerGroup;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekUnionValue;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekUserValue;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.bussiness.beautyescort.BeautyEscortBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.EscortServerGroupMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class BeautyEscortMgr extends TempMgr {

    //weekId userId
    static Map<Integer, Map<Long, EscortWeekUserValue>> weekUserValueMap = new ConcurrentHashMap<>();           //玩家走镖数据

    //weekId unionUid
    static Map<Integer, Map<String, EscortWeekUnionValue>> weekUnionValueMap = new ConcurrentHashMap<>();       //商会走镖数据

    //需要上传美女总魅力值的玩家
    private static Map<Long, List<Integer>> needUploadUserInfoMap = new ConcurrentHashMap<>();

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        int thisWeekId = getThisWeekId();
        int preWeekId = getPreWeekId();
        Map<Integer, Map<Long, EscortWeekUserValue>> tempWeekUserValueMap = BeautyEscortBussiness.getWeekUserValueMap(preWeekId, thisWeekId);
        weekUserValueMap = tempWeekUserValueMap;
        Map<Integer, Map<String, EscortWeekUnionValue>> tempWeekUnionValueMap = BeautyEscortBussiness.getWeekUnionValueMap(preWeekId, thisWeekId);
        weekUnionValueMap = tempWeekUnionValueMap;
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for (Map.Entry<Integer, Map<Long, EscortWeekUserValue>> mapEntry : weekUserValueMap.entrySet()) {
            for (Map.Entry<Long, EscortWeekUserValue> valueEntry : mapEntry.getValue().entrySet()) {
                if(valueEntry.getValue().isInsertOption()){
                    BeautyEscortBussiness.addEscortWeekUserValue(valueEntry.getValue());
                }else if(valueEntry.getValue().isUpdateOption()){
                    BeautyEscortBussiness.updateEscortWeekUserValue(valueEntry.getValue());
                }
            }
        }

        for (Map.Entry<Integer, Map<String, EscortWeekUnionValue>> mapEntry : weekUnionValueMap.entrySet()) {
            for (Map.Entry<String, EscortWeekUnionValue> valueEntry : mapEntry.getValue().entrySet()) {
                if(valueEntry.getValue().isInsertOption()){
                    BeautyEscortBussiness.addEscortWeekUnionValue(valueEntry.getValue());
                }else if(valueEntry.getValue().isUpdateOption()){
                    BeautyEscortBussiness.updateEscortWeekUnionValue(valueEntry.getValue());
                }
            }
        }

        return true;
    }

    public static boolean isFree(EscortDartTypeInfo typeInfo) {
        return "0".equals(typeInfo.getConsume()) || PropertyHelper.parseStringToProperty(typeInfo.getConsume()).isNothing();
    }

    /**
     * 分组是否开放
     */
    public static boolean groupOpen(){
        EscortServerGroup group = EscortServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        if(group != null && group.getEscortState() == 1){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 初始玩家每周走镖数据
     */
    public static EscortWeekUserValue initUserData(int weekId, long userId, String unionUid) {
        if(!weekUserValueMap.containsKey(weekId)){
            synchronized (weekUserValueMap){
                if(!weekUserValueMap.containsKey(weekId)){
                    weekUserValueMap.put(weekId, new ConcurrentHashMap<>());
                }
            }
        }
        Map<Long, EscortWeekUserValue> userValueMap = weekUserValueMap.get(weekId);
        if(!userValueMap.containsKey(userId)){
            synchronized (userValueMap){
                if(!userValueMap.containsKey(userId)){
                    EscortWeekUserValue userValue = new EscortWeekUserValue();
                    userValue.setUserId(userId);
                    userValue.setWeekId(weekId);
                    userValue.setUnionUid(unionUid);
                    userValue.setInsertOption();
                    //position
                    UnionMember unionMember = CrossUnionMgr.getUnionMember(unionUid, userId);
                    if(unionMember != null){
                        userValue.setPosition(unionMember.getPosition());
                    }
                    userValueMap.put(userId, userValue);
                }
            }
        }
        return getEscortWeekUserValue(weekId, userId);
    }

    public static EscortWeekUserValue getEscortWeekUserValueOrInit(int weekId, long userId){
        EscortWeekUserValue userValue = getEscortWeekUserValue(weekId, userId);
        if(userValue == null){
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if(userInfo == null){
                initUserData(weekId, userId, "");
            }else {
                initUserData(weekId, userId, userInfo.getUnionUid());
            }
        }
        return getEscortWeekUserValue(weekId, userId);
    }

    public static EscortWeekUserValue getEscortWeekUserValue(int weekId, long userId){
        if(!weekUserValueMap.containsKey(weekId)){
            return null;
        }
        return weekUserValueMap.get(weekId).get(userId);
    }

    /**
     * 初始玩家每周数据
     */
    public static void initOnlineUserWeekData() {
        int weekId = BeautyEscortMgr.getThisWeekId();
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            initUserData(weekId, player.getUserId(), player.getUserInfo().getUnionUid());
        }
    }

    public static EscortWeekUnionValue getEscortWeekUnionValueNullable(int weekId, String unionUid){
        if(!weekUnionValueMap.containsKey(weekId)){
            return null;
        }
        return weekUnionValueMap.get(weekId).get(unionUid);
    }

    /**
     * 商会走镖数据
     */
    public static EscortWeekUnionValue getEscortWeekUnionValue(int weekId, String unionUid){
        if(!weekUnionValueMap.containsKey(weekId)){
            synchronized (weekUnionValueMap){
                if(!weekUnionValueMap.containsKey(weekId)){
                    weekUnionValueMap.put(weekId, new ConcurrentHashMap<>());
                }
            }
        }
        Map<String, EscortWeekUnionValue> unionValueMap = weekUnionValueMap.get(weekId);
        if(!unionValueMap.containsKey(unionUid)){
            synchronized (unionValueMap){
                if(!unionValueMap.containsKey(unionUid)){
                    EscortWeekUnionValue unionValue = new EscortWeekUnionValue();
                    unionValue.setWeekId(weekId);
                    unionValue.setUnionUid(unionUid);
                    unionValue.setInsertOption();
                    unionValueMap.put(unionUid, unionValue);
                }
            }
        }
        return unionValueMap.get(unionUid);
    }

    /**
     * 解散商会
     */
    public static void deleteUnion(List<Long> syncUserIdList, UnionInfo unionInfo) {
        int unionLv = UnionConfigMgr.getUnionLv(unionInfo.getExperience());
        if(unionLv < GameConfig.ESCORT_OPEN_UNION_LEVEL){
            return;
        }
        int thisWeekId = getThisWeekId();
        for (long userId : syncUserIdList) {
            EscortWeekUserValue userValue = getEscortWeekUserValue(thisWeekId, userId);
            if(userValue == null){
                return;
            }
            synchronized (userValue) {
                userValue.setUnionUid("");
                userValue.setPosition(0);
            }
        }
        if(syncUserIdList.size() > 0){
            int weekId = BeautyEscortMgr.getThisWeekId();
            changeUnionRank(unionInfo.getUnionUid(), syncUserIdList.get(0), UnionRiseRankArgs.NOTIFY_TYPE_DELETE, weekId);
        }

        Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid());
        if(unionMemberMap != null){
            //玩家商会数据变更
            BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.Builder syncMsg = BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.newBuilder();
            for (Map.Entry<Long, CrossUnionMember> memberEntry : unionMemberMap.entrySet()) {
                syncMsg.addMember(memberEntry.getKey());
            }
            syncMsg.setType(4);
            GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_USER_UNION_INFO_SYNC, syncMsg));
        }
    }

    /**
     * 创建商会
     */
    public static void createUnion(UnionInfo unionInfo, long userId) {
        int thisWeekId = getThisWeekId();
        EscortWeekUserValue userValue = getEscortWeekUserValue(thisWeekId, userId);
        if(userValue == null){
            return;
        }
        userValue.setUnionUid(unionInfo.getUnionUid());
        userValue.setPosition(eUnionPosition.Master.getValue());

        //玩家商会数据变更
        BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.Builder syncMsg = BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.newBuilder();
        syncMsg.addMember(userId);
        syncMsg.setType(1);
        syncMsg.setUnionLv(1);
        syncMsg.setUnionUid(unionInfo.getUnionUid());
        UnionProto.UnionBaseTempMsg.Builder unionBuilder = UnionProto.UnionBaseTempMsg.newBuilder();
        unionBuilder.setUnionAliasId(unionInfo.getUnionAliasId());
        unionBuilder.setUnionName(unionInfo.getUnionName());
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_USER_UNION_INFO_SYNC, syncMsg));


        //榜单
        int unionLv = UnionConfigMgr.getUnionLv(unionInfo.getExperience());
        if(unionLv < GameConfig.ESCORT_OPEN_UNION_LEVEL){
            return;
        }
        changeUnionRank(unionInfo.getUnionUid(), userId, UnionRiseRankArgs.NOTIFY_TYPE_JOIN, thisWeekId);
    }

    /**
     * 加入商会
     * @param unionInfo
     * @param userId
     */
    public static void unionMemberJoin(UnionInfo unionInfo, long userId) {
        int unionLv = UnionConfigMgr.getUnionLv(unionInfo.getExperience());
        if(unionLv < GameConfig.ESCORT_OPEN_UNION_LEVEL){
            return;
        }
        int thisWeekId = getThisWeekId();
        EscortWeekUserValue userValue = getEscortWeekUserValueOrInit(thisWeekId, userId);
        if(userValue == null){
            return;
        }
        userValue.setUnionUid(unionInfo.getUnionUid());
        UnionMember unionMember = CrossUnionMgr.getUnionMember(unionInfo.getUnionUid(), userId);
        if(unionMember != null){
            userValue.setPosition(unionMember.getPosition());
        }
        changeUnionRank(unionInfo.getUnionUid(), userId, UnionRiseRankArgs.NOTIFY_TYPE_JOIN, thisWeekId);

        //玩家商会数据变更
        BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.Builder syncMsg = BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.newBuilder();
        syncMsg.addMember(userId);
        syncMsg.setType(2);
        syncMsg.setUnionLv(unionLv);
        syncMsg.setUnionUid(unionInfo.getUnionUid());
        UnionProto.UnionBaseTempMsg.Builder unionBuilder = UnionProto.UnionBaseTempMsg.newBuilder();
        unionBuilder.setUnionAliasId(unionInfo.getUnionAliasId());
        unionBuilder.setUnionName(unionInfo.getUnionName());
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_USER_UNION_INFO_SYNC, syncMsg));
    }

    /**
     * 玩家退出商会
     * @param userId
     * @param unionInfo
     */
    public static void quitUnion(long userId, UnionInfo unionInfo) {
        int unionLv = UnionConfigMgr.getUnionLv(unionInfo.getExperience());
        if(unionLv < GameConfig.ESCORT_OPEN_UNION_LEVEL){
            return;
        }
        int thisWeekId = getThisWeekId();
        EscortWeekUserValue userValue = getEscortWeekUserValue(thisWeekId, userId);
        if(userValue == null){
            return;
        }
        synchronized (userValue) {
            userValue.setUnionUid("");
            userValue.setPosition(0);
        }
        //刷新榜单
        changeUnionRank(unionInfo.getUnionUid(), userId, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, thisWeekId);

        //玩家商会数据变更
        BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.Builder syncMsg = BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.newBuilder();
        syncMsg.addMember(userId);
        syncMsg.setType(3);
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_USER_UNION_INFO_SYNC, syncMsg));
    }

    /**
     * 商会升级
     */
    public static void upgradeUnion(UnionInfo unionInfo) {
        int unionLv = UnionConfigMgr.getUnionLv(unionInfo.getExperience());
        if(unionLv < GameConfig.ESCORT_OPEN_UNION_LEVEL){
            return;
        }
        Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid());
        if(unionMemberMap != null){
            //玩家商会数据变更
            BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.Builder syncMsg = BeautyEscortProto.CrossBeautyEscortUserUnionInfoChangeSyncMsg.newBuilder();
            for (Map.Entry<Long, CrossUnionMember> memberEntry : unionMemberMap.entrySet()) {
                syncMsg.addMember(memberEntry.getKey());
            }
            syncMsg.setType(5);
            syncMsg.setUnionLv(unionLv);
            GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_USER_UNION_INFO_SYNC, syncMsg));
        }
    }

    /**
     * 通知跨服结算走镖
     */
    private static void notifyCrossQuitUnion(long userId, String unionUid) {
        BeautyEscortProto.CrossBeautyEscortNotifyQuitUnionReqMsg.Builder notifyMsg = BeautyEscortProto.CrossBeautyEscortNotifyQuitUnionReqMsg.newBuilder();
        notifyMsg.setUserId(userId);
        notifyMsg.setUnionUid(unionUid);
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_QUIT_UNION_SETTLEMENT, notifyMsg));
    }

    /**
     * 玩家被踢
     * @param unionInfo
     * @param kickUserId
     */
    public static void kickPlayer(UnionInfo unionInfo, long kickUserId) {
        quitUnion(kickUserId, unionInfo);
    }

    /**
     * 商会职位变更
     * @param unionInfo
     */
    public static void memberPositionChange(UnionInfo unionInfo, long userId, int position) {
        int thisWeekId = getThisWeekId();
        EscortWeekUserValue userValue = getEscortWeekUserValue(thisWeekId, userId);
        if(userValue == null){
            return;
        }
        userValue.setPosition(position);
    }

    /**
     * 商会冲榜
     * @param type 0:normal 1:join 2:remove 3:del
     */
    public static void changeUnionRank(String unionUid, long userId, int type, int weekId) {
        if(StringUtils.isNullOrEmpty(unionUid)){
            return;
        }
        Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
        if(unionMemberMap == null){
            return;
        }

        long val = 0;
        for (Map.Entry<Long, CrossUnionMember> memberEntry : unionMemberMap.entrySet()) {
            //保证跨周未登录的玩家，也可以领上周奖励
            EscortWeekUserValue userValue = BeautyEscortMgr.getEscortWeekUserValueOrInit(weekId, memberEntry.getKey());
            if(userValue == null){
                continue;
            }
            val += userValue.getValue();
        }
        EscortWeekUnionValue unionValue = BeautyEscortMgr.getEscortWeekUnionValue(weekId, unionUid);
        synchronized (unionValue){
            unionValue.setValue(val);
            if(type == UnionRiseRankArgs.NOTIFY_TYPE_JOIN || type == UnionRiseRankArgs.NOTIFY_TYPE_NORMAL) {
                unionValue.addMemberUserId(userId);
            }else if(type == UnionRiseRankArgs.NOTIFY_TYPE_REMOVE){
                unionValue.removeMemberUserId(userId);
            }else if(type == UnionRiseRankArgs.NOTIFY_TYPE_DELETE){
                unionValue.setUserList(new ArrayList<>());
            }

            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if(unionInfo != null && userInfo != null){
                UnionBaseInfo unionBaseInfo = CrossUnionMgr.parseUnionBaseInfo(unionInfo);
                unionValue.setUnionBaseInfo(unionBaseInfo);
            }
        }
        if(val == 0 && type != UnionRiseRankArgs.NOTIFY_TYPE_DELETE && type != UnionRiseRankArgs.NOTIFY_TYPE_REMOVE){//0分不冲榜
            return;
        }
        EscortWeekUserValue userValue = BeautyEscortMgr.getEscortWeekUserValueOrInit(weekId, userId);
        BeautyEscortProto.BeautyEscortWeekUserValueChangeMsg.Builder changMsg = BeautyEscortProto.BeautyEscortWeekUserValueChangeMsg.newBuilder();
        changMsg.setType(type);
        changMsg.setUnionUid(unionUid);
        changMsg.setWeekId(weekId);
        changMsg.setUnion(UnionBasePb.parseUnionBaseTempMsg(unionValue.getUnionBaseInfo()));
        BeautyEscortProto.BeautyEscortWeekUserValueTemp.Builder userValueBuilder = BeautyEscortProto.BeautyEscortWeekUserValueTemp.newBuilder();
        userValueBuilder.setUserId(userId);
        userValueBuilder.setValue(userValue.getValue());
        userValueBuilder.setModifyTime(userValue.getModifyTime());
        changMsg.setUserValue(userValueBuilder);
        GamePlayerMgr.sendPacket(userId, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_UPLOAD_USER_VALUE_CHANGE, changMsg));
        /*
        BeautyEscortProto.CrossUnionWeekRankTemp.Builder rankBuilder = BeautyEscortProto.CrossUnionWeekRankTemp.newBuilder();
        rankBuilder.setValue(String.valueOf(val));
        rankBuilder.setUnionUid(unionUid);
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        UnionBaseInfo unionBaseInfo = UnionHelper.parseUnionBaseInfo(unionInfo, UserMgr.getUserInfo(unionInfo.getMasterUserId()), GameServer.getInstance().getServerId());
        rankBuilder.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(unionBaseInfo));

        BeautyEscortProto.CrossUnionWeekRankChangeReqMsg.Builder changeMsg = BeautyEscortProto.CrossUnionWeekRankChangeReqMsg.newBuilder();
        changeMsg.setWeekId(weekId);
        changeMsg.addRank(rankBuilder);
        GamePlayerMgr.sendChannelPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_UNION_RANK_CHANGE, changeMsg));*/
    }

    /**
     * 获取美女总魅力值
     */
    public static long getBeautyTotalGlamour(GamePlayer player) {
        long totalGlamour = 0;
        Map<Integer, UserBeauty> beautyMap = player.getModule(BeautyModule.class).getBeautyMap();
        for (Map.Entry<Integer, UserBeauty> beautyEntry : beautyMap.entrySet()) {
            totalGlamour += beautyEntry.getValue().getTotalGlamour();
        }
        return totalGlamour;
    }

    /**
     * 玩家变更美女魅力值
     */
    public static void addNeedUploadUserInfo(GamePlayer player, int beautyId){
        if(StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
            return;
        }
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
        if (unionInfo == null) {
            return;
        }
        if(UnionConfigMgr.getUnionLv(unionInfo.getExperience()) < GameConfig.ESCORT_OPEN_UNION_LEVEL) {
            return;
        }
        //玩家是否有解锁
        //if(!SystemOpenMgr.systemOpen(player, eSystemId.BeautyEscort.getValue())){
        //    return;
        //}
        synchronized (needUploadUserInfoMap) {
            if(!needUploadUserInfoMap.containsKey(player.getUserId())) {
                needUploadUserInfoMap.put(player.getUserId(), new ArrayList<>());
            }
            List beautyList = needUploadUserInfoMap.get(player.getUserId());
            if(!beautyList.contains(beautyId)){
                beautyList.add(beautyId);
            }
        }
    }

    public static Map<Long, List<Integer>> getNeedUploadUserInfoMap() {
        return needUploadUserInfoMap;
    }

    /**
     * 同步玩家数据
     */
    public static void uploadUserInfo(){
        Map<Long, List<Integer>> temp;
        synchronized (needUploadUserInfoMap) {
            temp = needUploadUserInfoMap;
            needUploadUserInfoMap = new ConcurrentHashMap<>();
        }
        if(temp.size() > 0){
            BeautyEscortProto.CrossBeautyEscortPlayInfoSyncMsg.Builder syncMsg = BeautyEscortProto.CrossBeautyEscortPlayInfoSyncMsg.newBuilder();
            for (long uid : temp.keySet()) {
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(uid);
                if(player == null){
                    continue;
                }
                BeautyEscortProto.CrossBeautyEscortPlayInfoTemp.Builder builder = BeautyEscortProto.CrossBeautyEscortPlayInfoTemp.newBuilder();
                builder.setUserId(player.getUserId());
                builder.setTotalGlamour(getBeautyTotalGlamour(player));
                BeautyModule beautyModule = player.getModule(BeautyModule.class);
                for (int beautyId : temp.get(uid)) {
                    UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
                    if(userBeauty != null){
                        builder.addBeauty(getBeautyDispatchBuilder(beautyId, userBeauty.getTotalGlamour()));
                    }
                }
                syncMsg.addInfo(builder);
            }
            GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_SYNC_PLAY_INFO, syncMsg));
        }
    }


    /**
     * 获取区服所在分组
     */
    public static int getGroupId(long serverId) {
        EscortServerGroup serverGroup = EscortServerGroupMgr.getServerGroupByServerId(serverId);
        if(serverGroup != null && serverGroup.getEscortState() == 1){
            return serverGroup.getGroupId();
        }
        return (int)serverId;
    }

    /**
     * 本周id
     */
    public static int getThisWeekId(){
        Calendar now = Calendar.getInstance();
        //如果是周天
        if(now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            long zeroTime = DateHelper.getTodayZeroTimeStamp();
            //22点后，返回后一天零点
            if(now.getTimeInMillis() >= (zeroTime + ConfigMgr.getEscortingEndMinute() * 60 * 1000)){
                now.setTimeInMillis(zeroTime);
                now.add(Calendar.HOUR, 24);
            }else {
                Date monday = DateHelper.getThisWeekMonday(now.getTime());
                now.setTime(monday);
            }
        }else {
            Date monday = DateHelper.getThisWeekMonday(now.getTime());
            now.setTime(monday);
        }
        return Integer.valueOf(DateHelper.getPyyyyMMddDateString(now.getTimeInMillis() / 1000));
    }

    /**
     * 获取所在组的区服列表(没有分组即独自成组)
     */
    public static Set<Long> getGroupServer(int groupId){
        Set<Long> serverSet = new HashSet<>();
        EscortServerGroup serverGroup = EscortServerGroupMgr.getServerGroup(groupId);
        if(serverGroup == null || serverGroup.getEscortState() == 0){
            serverSet.add((long)groupId);
        }else{
            serverSet.addAll(serverGroup.getServerList());
        }
        return serverSet;
    }

    /**
     * 前一周id
     */
    public static int getPreWeekId() {
        Calendar now = Calendar.getInstance();
        //如果是周天
        if(now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            long zeroTime = DateHelper.getTodayZeroTimeStamp();
            //22点后，返回本周零点
            if(now.getTimeInMillis() >= (zeroTime + ConfigMgr.getEscortingEndMinute() * 60 * 1000)){
                Date monday = DateHelper.getThisWeekMonday(now.getTime());
                now.setTime(monday);
            }else {
                Date monday = DateHelper.getThisWeekMonday(now.getTime());
                now.setTime(monday);
                now.add(Calendar.HOUR, -7 * 24);
            }
        }else {
            Date monday = DateHelper.getThisWeekMonday(now.getTime());
            now.setTime(monday);
            now.add(Calendar.HOUR, -7 * 24);
        }
        return Integer.valueOf(DateHelper.getPyyyyMMddDateString(now.getTimeInMillis() / 1000));
    }

    /**
     * 美女派遣Builder
     */
    public static List<BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp> parseBeautyDispatchBuilderList(List<EscortBeautyDispatch> dispatchList){
        List<BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp> list = new ArrayList<>(dispatchList.size());
        for (EscortBeautyDispatch  dispatch : dispatchList) {
            BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp.Builder builder = BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp.newBuilder();
            builder.setBeautyId(dispatch.getBeautyId());
            builder.setGlamour(dispatch.getGlamour());
            builder.setLeftDispatchTime(dispatch.getLeftDispatchTime());
            list.add(builder.build());
        }
        return list;
    }

    /**
     * 美女派遣Builder
     */
    public static List<EscortBeautyDispatch> parseBeautyDispatch(List<BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp> tempList){
        List<EscortBeautyDispatch> result = new ArrayList<>();
        for(BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp temp : tempList) {
            EscortBeautyDispatch dispatch = new EscortBeautyDispatch();
            dispatch.setBeautyId(temp.getBeautyId());
            dispatch.setGlamour(temp.getGlamour());
            dispatch.setLeftDispatchTime(temp.getLeftDispatchTime());
            result.add(dispatch);
        }
        return result;
    }

    public static BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp.Builder getBeautyDispatchBuilder(int beautyId, int glamour) {
        BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp.Builder builder = BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp.newBuilder();
        builder.setBeautyId(beautyId);
        builder.setGlamour(glamour);
        return builder;
    }

    /**
     * 榜单成员贡献Builder
     */
    @Deprecated
    public static List<BeautyEscortProto.EscortUnionRankMemberDataTempMsg> getUnionRankMemberDataTemp(int weekId, String unionUid){
        List<BeautyEscortProto.EscortUnionRankMemberDataTempMsg> dataTempMsgList = new ArrayList<>();
        EscortWeekUnionValue unionValue = BeautyEscortMgr.getEscortWeekUnionValueNullable(weekId, unionUid);
        if(unionValue == null){
            return dataTempMsgList;
        }
        int size = unionValue.getUserList().size();
        for(int i = 0; i < size; i++){
            long uid = unionValue.getUserList().get(i);
            EscortWeekUserValue userValue = BeautyEscortMgr.getEscortWeekUserValue(weekId, uid);
            if(userValue == null || userValue.getValue() <= 0){
                continue;
            }
            BeautyEscortProto.EscortUnionRankMemberDataTempMsg.Builder builder = BeautyEscortProto.EscortUnionRankMemberDataTempMsg.newBuilder();
            builder.setModifyTime(userValue.getModifyTime());
            builder.setUserId(uid);
            builder.setValue(String.valueOf(userValue.getValue()));
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(uid, GameServer.getInstance().getServerId());
            builder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            dataTempMsgList.add(builder.build());
        }
        return dataTempMsgList;
    }

    /**
     *
     */
    public static void uploadWeekRankValue(){
        if(!CrossUnionMgr.isAllSync()){
            getLogger().info("wait CrossUnionMgr allSync");
            return;
        }
        int preWeekId = getPreWeekId();
        int thisWeekId = getThisWeekId();
        List<Integer> weekList = new ArrayList<>();
        weekList.add(preWeekId);
        weekList.add(thisWeekId);
        BeautyEscortProto.BeautyEscortUploadValueMsg.Builder uploadMsg = BeautyEscortProto.BeautyEscortUploadValueMsg.newBuilder();

        for (int weekId : weekList) {
            List<Long> hasUnionUserIdList = new ArrayList<>();
            Map<String, EscortWeekUnionValue> unionMap = weekUnionValueMap.get(weekId);
            if(unionMap == null){
                continue;
            }
            BeautyEscortProto.BeautyEscortWeekValueTemp.Builder weekDataTemp = BeautyEscortProto.BeautyEscortWeekValueTemp.newBuilder();
            weekDataTemp.setWeekId(weekId);
            for (Map.Entry<String, EscortWeekUnionValue> valueEntry : unionMap.entrySet()) {
                EscortWeekUnionValue unionValue = valueEntry.getValue();
                //商会数据
                BeautyEscortProto.BeautyEscortWeekUnionValueTemp.Builder unionDataTemp = BeautyEscortProto.BeautyEscortWeekUnionValueTemp.newBuilder();
                unionDataTemp.setUnionUid(unionValue.getUnionUid());
                unionDataTemp.setValue(unionValue.getValue());
                unionDataTemp.setModifyTime(0L);
                CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionValue.getUnionUid());
                if(null != unionInfo){
                    unionDataTemp.setUnion(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                }else {
                    continue;
                }

                Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionValue.getUnionUid());
                if(memberMap != null){
                    for (Map.Entry<Long, CrossUnionMember> memberEntry : memberMap.entrySet()) {
                        CrossUnionMember member = memberEntry.getValue();
                        if(GameServer.getInstance().getServerId() != member.getServerId()){
                            continue;
                        }
                        EscortWeekUserValue userValue = getEscortWeekUserValue(weekId, member.getUserId());
                        if(userValue != null){
                            //商会成员数据
                            BeautyEscortProto.BeautyEscortWeekUserValueTemp.Builder userDataTemp = BeautyEscortProto.BeautyEscortWeekUserValueTemp.newBuilder();
                            userDataTemp.setValue(userValue.getValue());
                            userDataTemp.setUserId(userValue.getUserId());
                            userDataTemp.setModifyTime(userValue.getModifyTime());
                            unionDataTemp.addUser(userDataTemp);
                            hasUnionUserIdList.add(userValue.getUserId());
                        }
                    }
                }
                if(unionDataTemp.getUserCount() > 0) {
                    weekDataTemp.addUnion(unionDataTemp);
                    getLogger().info("weekDataTemp {}", weekDataTemp.getUnionCount());
                }
            }
            if(weekDataTemp.getUnionCount() > 0) {
                uploadMsg.addWeek(weekDataTemp);
            }
        }

        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_UPLOAD_VALUE, uploadMsg));
        getLogger().info("uploadMsg {}", uploadMsg.getWeekCount());
    }
}
